package cc.site.main.service.account;


import cc.site.common.exception.FineException;
import cc.site.common.utils.R;

import cc.site.main.bean.TblUser;
import cc.site.main.service.TblUserService;
import cc.site.main.type.UserType;
import cc.site.security.bean.LoginUser;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class UserDetailServiceImpl implements UserDetailsService {

    @Autowired
    private TblUserService userService;


    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        LambdaQueryWrapper<TblUser> w = new LambdaQueryWrapper<>();
        w.eq(TblUser::getUsername, s);
        TblUser user = userService.getOne(w);
        if (user == null) {
            throw new FineException(R.ERROR, "找不到用户");
        }

        // 封装权限信息
        List<String> list = new ArrayList<>();
        if (user.getType().equals(UserType.Manager)) {
            list.add("admin");
        }
        list.add("normal");

        return new LoginUser(user, list);
    }
}
